package com.xinnet.mms.web;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xinnet.core.utils.page.PageIndex;
import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.dept.service.DeptService;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.Role;
import com.xinnet.mms.login.service.EmpService;
import com.xinnet.mms.role.service.RoleService;
import com.xinnet.mms.utils.AgainPageView;

@Controller
@RequestMapping(value = "/ruleSet/employeeSet")
public class EmployeeSetController {
	
	@Autowired
	EmpService empService;
	@Autowired
	DeptService deptService;
	@Autowired
	RoleService roleService;
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/employeeSetPage")
	public String employeeSetPage(HttpServletRequest request,HttpServletResponse response){
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		String deptId = currentUser.getId();
		String deptIdStr = deptService.getChildNodeId(deptId);
		List<Emp> empList = empService.getEmpListByDeptIdStr(deptIdStr);
		
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		if(empList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(empList.size(), empList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,empList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),empList);
		}
		
		List<Dept> deptList = deptService.getDeptListByDeptIdStr(deptIdStr);
		List<Role> roleList = roleService.getRoleList();
		request.setAttribute("deptList", deptList);
		//String deptJson  = deptService.getDeptTree();
		String deptJson  = "["+deptService.getDeptTreeByDeptId(deptId)+"]";
		request.setAttribute("deptJson", deptJson);
		request.setAttribute("roleList", roleList);
		request.setAttribute("pageView", pageView);
		PageIndex pageIndex = pageView.getPageindex();
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("isQuery", false);
		return "/ruleSet/employeeSet";
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/getEmpList")
	public String  getEmpList(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		String ename = request.getParameter("ename");
		String contact = request.getParameter("contact");
		String mail = request.getParameter("mail");
		String roleId = request.getParameter("roleId");
		String deptId = request.getParameter("deptId");
		Emp emp =  new Emp();
		if(ename!=null && !"".equals(ename)){
			emp.setEname(ename);
		}
		if(contact!=null && !"".equals(contact)){
			emp.setPhone(contact);
		}
		if(mail!=null && !"".equals(mail)){
			emp.setEmail(mail);
		}
		if(roleId!=null && !"".equals(roleId)){
			emp.setRolId(roleId);
		}
		if(deptId!=null && !"".equals(deptId)){
			String deptIdStr = deptService.getChildNodeId(deptId);
			emp.setDeptIdStr(deptIdStr);
		}else{
			Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
			String tempDeptId = currentUser.getId();
			String deptIdStr = deptService.getChildNodeId(tempDeptId);
			emp.setDeptIdStr(deptIdStr);	
		}
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		List<Emp> empList = empService.getEmpList(emp);
		if(empList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(empList.size(), empList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,empList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),empList);
		}
		List<Dept> deptList = deptService.getDeptList();
		List<Role> roleList = roleService.getRoleList();
		request.setAttribute("isQuery", true);
		request.setAttribute("pageView", pageView);
		if(!"".equals(roleId)){
			request.setAttribute("roleId", roleId);
		}
		if(!"".equals(deptId)){
			request.setAttribute("deptId", deptId);
		}
		if(!"".equals(ename)){
			request.setAttribute("ename", ename);
		}
		request.setAttribute("contact", contact);
		request.setAttribute("mail", mail);
		
		request.setAttribute("deptList", deptList);
		request.setAttribute("roleList", roleList);
		Dept dept = deptService.getDeptByDeptId(deptId);
		request.setAttribute("dept", dept);
		//String deptJson  = deptService.getDeptTree();
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		deptId = currentUser.getId();
		String deptJson  = "["+deptService.getDeptTreeByDeptId(deptId)+"]";
		request.setAttribute("deptJson", deptJson);
		
		return "/ruleSet/employeeSet";
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/updateEmp")
	public String  updateEmp(HttpServletRequest request,HttpServletResponse response){
		String empno = request.getParameter("empno");
		String repcustomerCount = request.getParameter("repcustomerCount");
		String customerLimit = request.getParameter("customerLimit");
		String customerLowerLimit = request.getParameter("customerLowerLimit");
		String isSysPush = request.getParameter("isSysPush");
		String isArtificialPush = request.getParameter("isArtificialPush");
		String receiveCustom = request.getParameter("receiveCustomHidden");
		Emp emp = new Emp();
		emp.setEmpno(empno);
		if(repcustomerCount!=null && !"".equals(repcustomerCount)){
			emp.setRepcustomerCountApproval(Integer.parseInt(repcustomerCount));
		}
		if(customerLimit!=null && !"".equals(customerLimit)){
			emp.setCustomerLimitApproval(Integer.parseInt(customerLimit));
		}
		if(customerLowerLimit!=null && !"".equals(customerLowerLimit)){
			emp.setCustomerLowerLimitApproval(Integer.parseInt(customerLowerLimit));
		}
		if(isSysPush!=null && !"".equals(isSysPush)){
			emp.setIsSysPushApproval(Integer.parseInt(isSysPush));
		}
		if(isArtificialPush!=null && !"".equals(isArtificialPush)){
			emp.setIsArtificialPushApproval(Integer.parseInt(isArtificialPush));
		}
		if(receiveCustom!=null && !"".equals(receiveCustom)) {
			emp.setReceiveCustomApproval(Integer.parseInt(receiveCustom));
		}
		emp.setApprovalStatus(2);
		empService.updateEmp(emp);	
		
		return "redirect:/ruleSet/employeeSet/employeeSetPage";
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/bathUpdateEmp")
	public String  bathUpdateEmp(HttpServletRequest request,HttpServletResponse response){
		String empnoStr = request.getParameter("empnoStr");
		if(empnoStr==null || "".equals(empnoStr)){
			return "redirect:/ruleSet/employeeSet/employeeSetPage";
		}
		String[] empnoArray= empnoStr.split(",");
		String repcustomerCount = request.getParameter("repcustomerCount");
		String customerLimit = request.getParameter("customerLimit");
		String customerLowerLimit = request.getParameter("customerLowerLimit");
		String isSysPush = request.getParameter("isSysPush");
		String isArtificialPush = request.getParameter("isArtificialPush");
		String receiveCustom=request.getParameter("receiveCustomHidden");
		if(empnoArray==null || empnoArray.length==0){
			return "redirect:/ruleSet/employeeSet/employeeSetPage";
		}
		for (int i = 0; i < empnoArray.length; i++) {
			Emp emp = new Emp();
			String empno = empnoArray[i];
			emp.setEmpno(empno);
			if(repcustomerCount!=null && repcustomerCount!=""){
				emp.setRepcustomerCountApproval(Integer.parseInt(repcustomerCount));
			}
			if(customerLimit!=null && customerLimit!=""){
				emp.setCustomerLimitApproval(Integer.parseInt(customerLimit));
			}
			if(customerLowerLimit!=null && customerLowerLimit!=""){
				emp.setCustomerLowerLimitApproval(Integer.parseInt(customerLowerLimit));
			}
			if(isSysPush!=null && isSysPush!=""){
				emp.setIsSysPushApproval(Integer.parseInt(isSysPush));
			}
			if(isArtificialPush!=null && isArtificialPush!=""){
				emp.setIsArtificialPushApproval(Integer.parseInt(isArtificialPush));
			}
			if(receiveCustom!=null && !receiveCustom.equals("")){
				emp.setReceiveCustomApproval(Integer.parseInt(receiveCustom));
			}
			emp.setApprovalStatus(2);
			empService.updateEmp(emp);
		}	
		return "redirect:/ruleSet/employeeSet/employeeSetPage";
	}
}
